import java.awt.Rectangle;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
/**
 * 
 *is the control class of Review Management window.
 */
public class ReviewManagementCTRL
{
	/** array of strings for data sent to database for review list retreive*/
	public String[] ReviewData = new String[2];
	/** array of strings for data sent to database for review itself*/
	public String[] Reviewinfo = new String[3];
	/** array of strings for data received from database for review itself*/
	public String[] insertReview = new String[6];
	/** array of strings for data received from database */
	public String[] res;
	public String Answer;

	/**
	 * this method sends DB a request to get unposted reviews list	
	 * returns array of reviews info 
	 */		
	public JComboBox o_GetReviews() 
	{
		String temp;
		ReviewData=new String[2];
		ReviewData[0]=Main.gusers.id;
		ReviewData[1]="getUnpostedReviews";				
		Main.log.connection.accept("getUnpostedReviews");
		Main.log.flag=false;
		while(Main.log.flag==false);
		Main.log.flag=false;		
		temp=ChatClient.getRs();
		if(temp.equals("NoReviews"))
			return null;		
		res=temp.split("\\*");	
		JComboBox ReviewsCombo;		
		ReviewsCombo = new JComboBox(res);				
		ReviewsCombo.setBounds(new Rectangle(120, 40, 280, 20));
		return ReviewsCombo;				
	}
	
	/**
	 * this method sends DB a request to get a selected review	
	 * after Retreive , update Gui with the retreived review 
	 */	
	public void  o_ViewReview(ReviewManagement revMgmntGui) 
	{
		int i;
		insertReview=new String[6];
		String temp,temp2[],temp3[];
		Reviewinfo=new String[3];
		Reviewinfo[0]=Main.gusers.id;
		Reviewinfo[1]="ReadUnpostedReview";
		Reviewinfo[2]=(String)revMgmntGui.ReviewCombo.getSelectedItem();
		temp2=Reviewinfo[2].split("\\-");
		temp3=temp2[2].split("\\(");
		temp2=temp3[1].split("\\)");		
		Reviewinfo[2]=temp2[0]; // Get rID from combobox selection
		Main.log.connection.accept("ReadUnpostedReview");
		Main.log.flag=false;
		while(Main.log.flag==false);
		Main.log.flag=false;		
		temp=ChatClient.getRs();
		res=temp.split("\\*");
		for(i=0; i<4; i++)
			insertReview[i+2]=res[i];		
		revMgmntGui.reviewText.setText(insertReview[4]);
		revMgmntGui.reviewText.updateUI();		
	}
	
	/**
	 * this method sends DB a request to Confirms/Remove a selected review
	 *	 ConfirmOrRemove=ConfirmReview   OR    ConfirmOrRemove=RemoveReview
	 * after Retreive , update Gui from the Confirmed/Removed review 
	 */	
	
	public void  o_ConfirmReview(ReviewManagement revMgmntGui,String ConfirmOrRemove ) 
	{
		insertReview[0]=Main.gusers.id;
		insertReview[1]=ConfirmOrRemove;
		insertReview[4]=(String)revMgmntGui.reviewText.getText();		 
		if (ConfirmOrRemove.equals("ConfirmReview"))
		{	
			if (insertReview[4].equals(""))
				JOptionPane.showMessageDialog(null, "Cant insert an empty review ","Input Error" ,JOptionPane.ERROR_MESSAGE);
			else if(!(JOptionPane.showConfirmDialog(null, "Confirm the selected Review? ","Confirmation" ,JOptionPane.OK_CANCEL_OPTION) == 0))
				return;
		}
		else if(!(JOptionPane.showConfirmDialog(null, "Are you sure you want to Remove the selected Review?\n\t\t\t(Review Cannot be restored!) ","Confirmation" ,JOptionPane.OK_CANCEL_OPTION) == 0))
			return;
		Main.log.connection.accept(ConfirmOrRemove);
		Main.log.flag=false;
		while(Main.log.flag==false);
		Main.log.flag=false;		
		if (ConfirmOrRemove.equals("ConfirmReview"))
			JOptionPane.showMessageDialog(null, "Selected Review was Posted ","Database success" ,JOptionPane.INFORMATION_MESSAGE);
		else
			JOptionPane.showMessageDialog(null, "Selected Review was Deleted ","Database success" ,JOptionPane.INFORMATION_MESSAGE);
		revMgmntGui.ResetLayout();
		revMgmntGui.panel.remove(revMgmntGui.ReviewCombo);
		revMgmntGui.ReviewCombo=null;
		JComboBox ReviewCombo=o_GetReviews();		
		if (ReviewCombo!=null)
		{
			revMgmntGui.panel.add(ReviewCombo,null);
			ReviewCombo.setVisible(true);
			ReviewCombo.setEnabled(true);
			revMgmntGui.ReviewCombo=ReviewCombo;
			revMgmntGui.panel.validate();
		}
		else
		{
			JOptionPane.showMessageDialog(null,"No Reviews to Confirm...","Database message",JOptionPane.ERROR_MESSAGE);
			o_Back(revMgmntGui.panel);
		}		
	}
	
	/**
	 * this method goes back to the Main screen
	 */	
	public void o_Back(JPanel panel) 
	{
		panel.setVisible(false);
		panel.setEnabled(false);
		Main.log.frame.setSize(550,515);
		Main.welcomeScreen.panel.setVisible(true);
		Main.welcomeScreen.panel.setEnabled(true);		
	}

	/**
	 * this method logs out of the system
	 */	
	public void o_logout(JPanel BookMngpanel)
	{
		if(JOptionPane.showConfirmDialog(null, "Are you sure you want to logout? ","Confirmation" ,JOptionPane.OK_CANCEL_OPTION) == 0)
		{
			BookMngpanel.setEnabled(false);
			BookMngpanel.setVisible(false);
			Main.log.loginData = new String[3];
			Main.log.loginData[0]=Main.gusers.id;
			Main.log.loginData[1]="logout";
			Main.log.loginData[2]=Main.gusers.id;		
			Main.log.connection= new ClientQuery(Main.serverIP,5555);
			Main.log.connection.accept("logout");
			Main.log.user.setText("");
			Main.log.pass.setText("");
			Main.log.frame.setSize(650,550);
			Main.log.panel.setEnabled(true);
			Main.log.panel.setVisible(true);
		}
	}

}

